package com.gyms.activity;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gyms.R;
import com.gyms.base.BaseActivity;
import com.bumptech.glide.Glide;
import com.gyms.constant.Constants;

import java.util.List;

import butterknife.BindView;
import weight.PinchImageView;

/**
 * 显示大图的页面
 * ltx
 */

public class ShowImagActivity extends BaseActivity {

    @BindView(R.id.vp_load_img)
    ViewPager mVpLoadImg;
    @BindView(R.id.showimage_text)
    TextView mShowimageText;
    private List<String> mVenuePic;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_show_ima;
    }

    @Override
    protected void initView() {

        int pagerNumber = getIntent().getExtras().getInt(Constants.FLAG_INTENT_PAGERNUMBER, 0);
        mVenuePic = (List<String>) getIntent().getExtras().getSerializable(Constants.FLAG_INTENT_VENUEPIC);
        mShowimageText.setText(pagerNumber +1+"/" + mVenuePic.size());
        setVpAdapter();
        mVpLoadImg.setCurrentItem(pagerNumber);

    }


    @Override
    protected void setListener() {
        mVpLoadImg.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mShowimageText.setText(position + 1 + "/" + mVenuePic.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }


        });

    }

    @Override
    protected void processLogic() {

    }

    private void setVpAdapter() {
        mVpLoadImg.setAdapter(new PagerAdapter() {

            @Override
            public int getCount() {
                return null!=mVenuePic?mVenuePic.size():0;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view.equals(object);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                final PinchImageView imageView = new PinchImageView(mContext);
                String mUrl=mVenuePic.get(position);
                /*ImageLoaderUtils.display(mContext,imageView,mUrl);*/
                imageView.setOnClickListener(new ImageClick());//给ImageView设置点击事件
                Glide.with(mContext).load(mUrl).fitCenter().into(imageView);
                container.addView(imageView);
                return imageView;
            }
        });//设置Viewpager的适配器
    }
    private class ImageClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            finish();
            overridePendingTransition(0, 0);
        }
    }



}
